<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册用户</title>
</head>
<body>
    <h1>注册用户</h1>
    <form>
        <ul type="none" style="padding-left:0px">
            <li>用户名：<input type="text" id="uname" value=""></li>
            <li>密 &nbsp&nbsp码：<input type="password" id="upwd" value=""></li>
            <li><input type="button" value="注册" onclick="reg()"></li>
        </ul>
    </form>
    <!-- http://127.0.0.1:3000/06_reg.html -->
    <script>
        function reg(){
            // http://127.0.0.1:3000/user/ref  POST
            //alert(123);
            //1获取用户输入的用户名和密码
            var uname = document.getElementById("uname");
            var upwd = document.getElementById("upwd");
            //alert(uname.value+"|"+upwd.value);
            //2创建对象AJAX
            var xhr = new XMLHttpRequest();
            //3指定接收函数
            xhr.onreadystatechange = function(){
                 //7接收数据
                 if(xhr.readyState===4&&xhr.status===200){
                     var obj = xhr.responseText;
                     if(obj==1){
                         alert("注册成功");
                     }else{
                         alert("注册失败");
                     }
                 }
            }
            //4指定服务器地址
            var url ="http://127.0.0.1:3000/user/ref";
            xhr.open("post",url,true);
            //5指定请求头
            xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
            //6发送请求
            xhr.send(`uname=${uname.value}&upwd=${upwd.value}`);
        }
    </script>
</body>
</html>